<?php
namespace app\admin\controller;

use app\admin\BaseController;
use think\facade\View;
use think\facade\Db;

class Category extends BaseController
{
	// 初始化
    protected function initialize()
    {
    	if(!session('?admin')){
    		return redirect('login/login');
    	}
    }
	
	/**
     * 分类列表
     * @access public
     */
    public function index()
	{
		$cate_list = Db::name('category')->order('sort desc')->select();
		$cate_list = recursive_make_tree($cate_list);										
		foreach($cate_list as $k=>$v){
			
		}				
		p($cate_list);
		View::assign('cate_list',$cate_list);
		return View::fetch('category/index');   
	}
	
	
	/**
     * 添加分类页面
     * @access public
     */
    public function add()
	{
		
		return View::fetch('category/add');   						
	}
	
	
	/**
     * 添加分类方法
     * @access public
     */
    public function doAdd()
	{
		$params = $this->request->post();
		$params = json_decode($params['data'],true);
		$result = Db::name('category')->insert($params);
		if($result){
			return make_json_response('',true,'添加成功');
		}else{
			return make_json_response('',false,'添加失败');
		}
	}
}
